package com.dingding.gmall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liudingding
 * @ClassName Cart
 * @description 购物车
 * @date 2021/7/29 9:37 下午
 */
public class Cart {

    /**
     * 购物项的列表(商品列表)
     */
    private List<CartItem> cartItems;

    /**
     * 商品数量
     */
    private Integer count;

    /**
     * 商品类型数量
     */
    private Integer countType;

    /**
     * 促销价
     */
    private BigDecimal reducePrice;

    /**
     * 需要支付的价格 = 总价 - 促销价(不是促销后的价)
     */
    private BigDecimal payPrice;

    public List<CartItem> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItem> cartItems) {
        this.cartItems = cartItems;
    }

    public Integer getCount() {
        int count = 0;
        if (!CollectionUtils.isEmpty(cartItems)){
            count = cartItems.stream().mapToInt(CartItem::getItemCount).sum();
        }
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getCountType() {
        int countType = 0;
        if (!CollectionUtils.isEmpty(cartItems)){
            countType = cartItems.size();
        }
        return countType;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getReducePrice() {
        // 计算购物车的总促销价
        if (!CollectionUtils.isEmpty(cartItems)){
            //各个购物项的促销价求和
            return cartItems.stream().map(CartItem::getItemReducePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        return new BigDecimal("0.00");
    }

    public void setReducePrice(BigDecimal reducePrice) {
        this.reducePrice = reducePrice;
    }

    public BigDecimal getPayPrice() {
        BigDecimal bigDecimal = new BigDecimal("0.00");
        // 计算需要支付的价格
        if (!CollectionUtils.isEmpty(cartItems)){
            payPrice = cartItems.stream().map(item -> {
                if (item.isCheck()) {
                    return item.getTotalPrice();
                }
                return bigDecimal;
            }).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal subtract = payPrice.subtract(getReducePrice());
            return subtract;
        }
        return bigDecimal;
    }

    public void setPayPrice(BigDecimal payPrice) {
        this.payPrice = payPrice;
    }
}
